<?php
class db_sqlite_lib {
	private $file = '';
	private $handle;
	private $trans_pending = false;
	private $error = '';
	private $results;

	public function __destruct(){
		$this->close();
	}
    public function __get($name){
        $fn_name = 'get_' . $name;
        if (method_exists($this, $fn_name)){
            return $this->$fn_name();
        }else{
            return null;
        }
    }
    public function __set($name, $value){
        $fn_name = 'set_' . $name;
        if (method_exists($this, $fn_name)){
            $this->$fn_name($value);
        } else {
        	$stop = '';
        }
    }
    private function get_results(){
    	return $this->results;
    }
    private function get_count(){
    	if (is_array($this->results))
    		return count($this->results);
    	else
    		return -1;
    }
    private function get_file(){
    	return $this->file;
    }
    private function get_error(){
    	return $this->error;
    }
    private function get_trans_pending(){
    	return $this->trans_pending;
    }
	private function get_open(){
		return ($this->handle != null);
	}
	private function set_file($value){
		$this->close();
		$this->file = $value;
	}
	function open(){
		$ret = '';
		if ($this->handle != null) {
			return $ret;
		}
	    try {
			$this->handle = new PDO("sqlite:" . $this->file);
			/*** set all errors to exceptions ***/
			$this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	    } catch(PDOException $e) {
	    	$this->handle = null;
			$ret = $e->getMessage();
			$this->error = $ret;
	    }
		return $ret;
	}
	function trans_begin(){
		$ret = 'Error';
		if ($this->handle === null) {
			$this->open();
		}
		if ($this->handle != null && !$this->trans_pending) {
			$ret = $this->exec('BEGIN');
			if ($ret == '') {
				$this->trans_pending = true;
				$ret = '';
			}
		}
		return $ret;
	}
	function esc($str){
		return sqlite_escape_string($str);
	}
	function trans_commit(){
		$ret = 'Error';
		if ($this->handle != null && $this->trans_pending) {
			$ret = $this->exec('COMMIT');
			if ($ret == '') {
				$this->trans_pending = false;
				$ret = '';
			}
		}
		return $ret;
	}
	function query($sql){
		$this->results = array();
		$res = null;
		$tmp_open = false;
		$this->error = '';
		if ($this->handle === null) {
			if ($this->open() == '')
				$tmp_open = true;
		}
		if ($this->handle != null) {
			try {
				$res = $this->handle->query($sql);
				$this->results = $res->fetchAll(PDO::FETCH_ASSOC);
				$res = null;
			} catch(PDOException $e) {
		    	$this->error = $e->getMessage();
		    }
		}
		if ($tmp_open) {
			$this->close();
		}
	    return count($this->results);
	}
	function exec($sql){
		$ret = -1;
		$this->error = '';
		$tmp_open = false;
		if ($this->handle === null) {
			if ($this->open() == '')
				$tmp_open = true;
		}
		if ($this->handle != null) {
			try {
				//error_reporting(E_ALL);
				$ret = $this->handle->exec($sql);
			} catch(PDOException $e) {
		    	$this->error = $e->getMessage();
		    } catch(Exception $e) {
		    	$this->error = $e->getMessage();
		    }
		}
		if ($tmp_open) {
			$this->close();
		}
	    return $ret;
	}
	function table_exists($table,$create_sql=''){
		if (!$this->get_db_exists() && $create_sql == '')
			return false;
		$sql = "SELECT name FROM sqlite_master WHERE type='table' and name='$table'";
		$fnd = false;
		if ($this->query($sql)) {
			$fnd = true;
		}
	    if (!$fnd && $create_sql != ''){
			if ($this->exec($create_sql) != -1) {
				return true;
			}
	    }
	    return $fnd;
	}
	function get_tables(){
		$sql = "SELECT name FROM sqlite_master WHERE type='table'";
		$this->query($sql);
	    return $this->results;
	}
	private function get_db_exists(){
		return file_exists($this->file);
	}
	function close(){
		$ret = 'Error';
		if ($this->handle != null) {
			if ($this->trans_pending) {
				$this->trans_commit();
			}
			$this->handle = null;
			$ret = '';
		} else {
			$ret = 'Database not open.';
		}
		return $ret;
	}
}
?>